package com.rongmai.web.bo;

import com.rongmai.db.dto.LabelDto;
import com.rongmai.db.mapper.LabelMapper;
import com.rongmai.db.model.Label;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Service
public class LabelBo {

    @Resource
    private LabelMapper labelMapper;

    public LabelDto labelTree(String code, Boolean isAll, String role) {
        LabelDto result = labelDto(code, role);
        children(result, isAll);
        return result;
    }

    public void children(LabelDto labelDto, Boolean isAll) {
        if (labelDto == null) {
            return;
        }
        List<LabelDto> children = getLabelDtoByParentId(labelDto.getId(), isAll);
        if (CollectionUtils.isEmpty(children)) {
            return;
        }
        labelDto.setChildren(children);
        for (LabelDto item : children) {
            children(item, isAll);
        }
    }

    public LabelDto labelDto(String code, String role) {
        Label label = getLabelByType(code, role);
        if (label == null) {
            return null;
        }
        LabelDto labelDto = new LabelDto();
        BeanUtils.copyProperties(label, labelDto);
        return labelDto;
    }

    public Label getLabelByType(String code, String role) {
        return labelMapper.getLabelByTypeAndRole(code, role);
    }

    public Label getLabelByType(String code) {
        return labelMapper.getLabelByType(code);
    }

    public List<Label> getLabelByParentId(Integer parentId, Boolean isSelf) {
        if (isSelf) {
            return labelMapper.getLabelByParentId4All(parentId);
        }
        return labelMapper.getLabelByParentId(parentId);
    }

    public List<LabelDto> getLabelDtoByParentId(Integer parentId, Boolean isAll) {
        List<Label> labels = getLabelByParentId(parentId, isAll);
        List<LabelDto> children = null;
        if (!CollectionUtils.isEmpty(labels)) {
            children = labels.stream().map(l -> {
                LabelDto labelDto = new LabelDto();
                BeanUtils.copyProperties(l, labelDto);
                return labelDto;
            }).collect(Collectors.toList());
        }
        return children;
    }

//    public List<LabelDto> getLabelDtoByCode(String code, boolean isAll) {
//        Label label = getLabelByType(code, role);
//        if (label == null) return Collections.emptyList();
//        return getLabelDtoByParentId(label.getId(), isAll);
//    }

    public Integer save(LabelDto labelDto) {
        Label label = new Label();
        BeanUtils.copyProperties(labelDto, label);
        label.setLevel(1);
        label.setStatus(1);
        label.setCreateTime(new Date());
        label.setUpdateTime(new Date());
        label.setIsSelf(true);
        label.setRole(labelDto.getRole());
        labelMapper.insert(label);
        return label.getId();
    }

    public void delete(Integer id) {
        labelMapper.delete(id);
    }

    public LabelDto get(Integer id) {
        if (id == null) return null;
        Label label = labelMapper.get(id);

        LabelDto labelDto = new LabelDto();
        BeanUtils.copyProperties(label, labelDto);
        return labelDto;
    }

    public List<String> getNameListByIds(String ids) {
        try {
            if (!StringUtils.isEmpty(ids)) {
                List<Label> labels = labelMapper.getByIds(Arrays.asList(ids.split(",")));
                return labels.stream().map(Label::getName).collect(Collectors.toList());
            }
        } catch (Exception e) {
            log.error("获取标签名称异常");
        }
        return new ArrayList<>();
    }

    public List<Label> queryByName(String queryKey) {
        return labelMapper.queryByName(queryKey);
    }

}
